package com.mew.chess.core.game.pokdeng.game;

import cn.hutool.core.util.StrUtil;
import com.mew.chess.core.common.handler.ChessStart;
import com.mew.chess.core.common.handler.ChessStartAnnotation;
import com.mew.chess.core.game.pokdeng.game.common.PokDengConstantValue;
import com.mew.chess.core.game.pokdeng.game.core.TableUtil;
import com.mew.common.dc.redis.GameRoomRedisService;
import com.mew.common.entity.config.Room;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author dylin
 */
@ChessStartAnnotation("POKDENG")
@Service
@Slf4j
public class StartPokDengService implements ChessStart {
    @Autowired
    private GameRoomRedisService gameRoomRedisService;

    @Override
    public void initializeGame(int gameId) throws Exception {
        PokDengGameManager gameManager=PokDengGameManager.getInstance();
        gameManager.setGameId(gameId);
        List<Room> roomList = gameRoomRedisService.findRoomByGameId(gameId);
        for (Room room : roomList) {
            gameManager.putController(initializeRoom(room));
        }
        log.info("-------PokDeng---gameId:{}---start-success-----", gameId);
    }
    private PokDengRoomManager initializeRoom(Room room){
        PokDengRoomManager roomManager=new PokDengRoomManager(room.getId(), StrUtil.splitToInt(room.getBetArrs(), ","));
        for (int i = 0; i < PokDengConstantValue.TABLE_INIT_DEFAULT; i++) {
            roomManager.putTableManger(TableUtil.initializeTable(room));
        }
        return roomManager;
    }
    public void initializeRoomManager(int gameId, int roomId){
        Room room = gameRoomRedisService.getRedisRoom(gameId, roomId);
        if (null == room){
            return;
        }
        PokDengGameManager.getInstance().putController(initializeRoom(room));
    }

}
